package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.mkt.BidAnalysisDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.domain.BidCurrencyDomain;
import top.infopub.mgr.mkt.jsonvo.BidAnalysisJsonVo;
import top.infopub.mgr.mkt.service.BidAnalysisService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.mgr.mkt.vo.BidCurrencyVo;
import top.infopub.model.mkt.BidAnalysis;
import top.infopub.model.mkt.BidInfo;
import top.infopub.model.mkt.BidReq;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.model.om.PaymentApply;
import top.infopub.model.om.PaymentApplyDetail;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


/**
 * 招标文件分析
 */
@Controller
@RequestMapping("/bidAnalysis")
public class BidAnalysiscController extends GenericFlowController {
    @Autowired
    private BidAnalysisService bidAnalysisService;

    @Autowired
    private ProProcessManageService proProcessManageService;

    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<BidCurrencyDomain> queryDataByCondition(BidCurrencyVo bidCurrencyVo,
                                                         ParamPage paramPage) {
        Page<BidCurrencyDomain> page = new Page<BidCurrencyDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bidAnalysisService.selectPage(page, bidCurrencyVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String id, String proCode){
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id", id);
        BidAnalysis bidAnalysis = bidAnalysisService.selectByPrimaryKey(id);
        request.setAttribute("proCode", bidAnalysis.getProCode());
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        if (bidAnalysis != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidAnalysis.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bidAnalysis.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(bidAnalysis.getProcessId()) && bidAnalysis.getProcessId().length() > 10) {
                request.setAttribute("processId", bidAnalysis.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(bidAnalysis.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidAnalysis.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "mkt/lay-biddingDocuments-detail";
    }

    @RequestMapping("/queryDetailById")
    @ResponseBody
    public RspVo queryDetailById(String id) {
        //招标文件分析详情信息
        BidAnalysisJsonVo bidAnsJson = bidAnalysisService.queryDetailById(id);
        return RspVo.getSuccessResponse(bidAnsJson);
    }

    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //立项审批
        proProcessManageService.auditProject(id, processId, status, auditMsg, null);
        return RspVo.getSuccessResponse();
    }

    /**
     * 编辑招标文件分析
     * @param request
     * @return
     */
    @RequestMapping("/edit")
    public @ResponseBody RspVo edit(HttpServletRequest request, String bodyStr, Integer submitType) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BidAnalysisJsonVo bidAnsJson = JSON.parseObject(str, BidAnalysisJsonVo.class);
        BidAnalysisDto bidAnsDto = bidAnsJson.getInfo();
        //更新投标文件分析-MKT_BID_ANALYSIS
        BidAnalysis bidAnalysis = new BidAnalysis();
        BeanUtils.copyProperties(bidAnsDto, bidAnalysis);

        //更新投标信息表-MKT_BID_INFO
        BidInfo bidInfo = new BidInfo();
        BeanUtils.copyProperties(bidAnsDto, bidInfo);

        //更新招标文件要求
        List<BidReq> yjList = bidAnsJson.getYjList();
        List<BidReq> jxList = bidAnsJson.getJxList();
        List<BidReq> ryList = bidAnsJson.getRyList();

        //更新付款申请-OM_PAYMENT_APPLY
        PaymentApply paymentApply = new PaymentApply();

        //更新付款申请明细-OM_PAYMENT_APPLY_DETAIL
        PaymentApplyDetail paymentApplyDetail = new PaymentApplyDetail();
        BeanUtils.copyProperties(bidAnsDto, paymentApplyDetail);

        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if(bidAnsJson.getFileAtt()!=null){
            String[] fileAtt = bidAnsJson.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, bidAnsDto.getProCode(), file);
                attachmentList.add(attachment);
            }
        }

        //调用service进行数据库操作
        RspVo rspVo = bidAnalysisService.editDetail(bidAnalysis, bidInfo, yjList, jxList, ryList, paymentApply, paymentApplyDetail, attachmentList, vo, submitType);
        return rspVo;
    }
}
